﻿/*include:js\declare*/
//TextArea高度自适应控件	<textarea autoheight="{LineHeight:20}" id="XXX"></textarea>
(function(){
var AutoHeight=function()
	{
	this.GetArgument(arguments[0],{Id:null,Event:null,LineHeight:null,MaxHeight:0,Timeout:200});
	var Element=fastCSharp.$Id(this.Id);
	if(this.LineHeight==null)	this.LineHeight=parseInt(0+Element.CssValue('lineHeight'));
	this.SetFunction=fastCSharp.ThisFunction(this,this.SetHeight);
	this.Div=this.$Create('div').Class(Element.Class()).Hide().To();
	this.Start(this.Event||{IsGetOnly:1});
	};
(fastCSharp.Functions.AutoHeight=AutoHeight).Inherit(fastCSharp.BaseFunction,{
Start:function(Event)
	{
	if(!Event.IsGetOnly)
		{
		var Element=fastCSharp.$Id(this.Id);
		if(!this.Element||Element.Element()!=this.Element.Element())
			{
			this.MinHeight=(this.Element=Element).ScrollHeight();
			Element.AddEvent('blur',fastCSharp.ThisFunction(this,this.Stop));
			Element.AddEvent('keypress,keyup',this.SetFunction);
			var Width=Element.Width(),Css=Element.Css();
			if(!fastCSharp.IsBorder)
				{
				Width-=parseInt(0+Css['border-left-width'],10)+parseInt(0+Css['border-right-width'],10);
				if(!fastCSharp.IsPadding)	Width-=parseInt(0+Css['padding-left'],10)+parseInt(0+Css['padding-right'],10);
				}
			this.Div.Css('width',Width+'px');
			if(this.Interval)	clearTimeout(this.Interval);
			this.IsStart=0;
			}
		if(!this.IsStart)
			{
			if(!this.Interval)	this.Interval=setTimeout(this.SetFunction,this.Timeout);
			this.IsStart=1;
			}
		}
	},
SetHeight:function()
	{
	if(this.IsStart)
		{
		this.Div.Html(this.Element.Value().ToHTML().replace(/\r\n/g,'<br />').replace(/[\r\n]/g,'<br />'));
		var Height=this.Div.ScrollHeight()+this.LineHeight;
		if(this.MaxHeight&&Height>this.MaxHeight)	Height=this.MaxHeight;
		this.Element.Css('height',Math.max(Height,this.MinHeight)+'px');
		if(this.Interval)	clearTimeout(this.Interval);
		this.Interval=setTimeout(this.SetFunction,this.Timeout);
		}
	},
Stop:function()
	{
	if(this.Interval)
		{
		clearTimeout(this.Interval);
		this.Interval=0;
		}
	this.IsStart=0;
	}
		});
new fastCSharp.Declare('AutoHeight','focus','Src');
})();